package com.tepth.appmanageservice.tools.ext;

public class ExtToString {
	public static String from2byte(byte data) {
		int z = data;
		z |= 256;
		String str = Integer.toBinaryString(z);
		int len = str.length();
		return str.substring(len - 8, len);
	}

	public static String fromStrbit(String bit) {
		String str = Integer.toBinaryString(Integer.valueOf(bit));
		int len = str.length();
		if (len == 3)
			str = "0" + str;
		else if (len == 2)
			str = "00" + str;
		else if (len == 1)
			str = "000" + str;
		return str;
	}

	public static String fromByteArray(byte[] data) {
		String str = "";
		for (int i = 0; i < data.length; i++)
			str += from2byte(data[i]);
		return str;
	}

	public static String fromByteArrayReverse(byte[] data) {
		String str = "";
		for (int i = 0; i < data.length; i++)
			str = from2byte(data[i]) + str;
		return str;
	}

	public static String fromByteArrayForHex(byte[] data) {
		String str = "";
		for (int i = 0; i < data.length; i++) {
			String hex = Integer.toHexString(data[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			str += hex.toUpperCase() + " ";
		}
		return str;
	}

	public static String fromByteArrayForCoding(byte[] data) {
		return new String(data);
	}

	public static String reverse(String newVal) {
		StringBuffer sb = new StringBuffer(newVal);
		return sb.reverse().toString();
	}
}